﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Windows.Forms;
using Campus.Main.Basic.Settings;
using Campus.Report.ReportForm;
using Microsoft.Reporting.WinForms;
using dbcntn;

namespace Campus.Main.Basic
{
    public partial class ImzaCizelgesi : DefaultForm
    {
        public ImzaCizelgesi()
        {
            InitializeComponent();
            aToDb=new ConnectionToDb(Helper.ConnectionString);
            Initialize();
        }
        public override void Initialize()
        {
            aToDb.AddItem(comboBox1,"pol");
            aToDb.AddItem(comboBox2,"yurtdef");
        }


        protected DateTime[] date { get; set; }
        protected int Pol { get; set; }
        protected void SetHeader(string[] temp)
        {
            for (int i=0;i<temp.Count();i++)
            {
                temp[i] = "par" + (i + 1);
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            
            var par = new List<ReportParameter>();
            string yurt =
                aToDb.SelectField("select description from yurtdef where id=" +
                                  Convert.ToString(comboBox2.SelectedValue));
            string pol = aToDb.SelectField("select description from pol where id=" + Convert.ToString(comboBox1.SelectedValue));
            if (dataGridView1.Rows.Count > 0)
            {
                string[] bk = new string[date.Count()];
                SetHeader(bk);
                for (int i = 0; i < bk.Count(); i++)
                {
                    par.Add(new ReportParameter(bk[i], date[i].ToString("dd.MM")));
                }
            }
            par.Add(new ReportParameter("yurt",yurt));
            par.Add(new ReportParameter("pol", pol));
            ReportForm f = new ReportForm();
            f.reportViewer1.ProcessingMode = ProcessingMode.Local;
            if (DateTime.Today.Day > 15)
            {
                switch (DateTime.DaysInMonth(DateTime.Today.Year, DateTime.Today.Month))
                {
                    case 30:
                        f.reportViewer1.LocalReport.ReportPath = "ReportTemplate/Imza.rdlc";
                        break;
                    case 31:
                        f.reportViewer1.LocalReport.ReportPath = "ReportTemplate/Imza31.rdlc";
                        break;
                    case 29:
                        f.reportViewer1.LocalReport.ReportPath = "ReportTemplate/Imza29.rdlc";
                        break;
                    case 28:
                        f.reportViewer1.LocalReport.ReportPath = "ReportTemplate/Imza28.rdlc";
                        break;
                }
            }
            else
            {
                f.reportViewer1.LocalReport.ReportPath = "ReportTemplate/Imza.rdlc";
            }
            ogrenci2006_dboDataSet1.Clear();
            f.reportViewer1.LocalReport.DataSources.Clear();
            f.reportViewer1.LocalReport.DataSources.Add(new ReportDataSource("DataSet1", dataGridView1.DataSource));
            f.reportViewer1.LocalReport.SetParameters(par);
            f.ShowDialog();
            button1.Enabled = false;

        }

        private void button3_Click(object sender, EventArgs e)
        {
            aToDb.AddItemDBGridView(dataGridView1, Query.ImzaCizelgesi + " and c.pol_id="+Pol+" and c.yurt_id="+YurtId+" "+ Query.OrderBy);
            if (DateTime.Today.Day <= 15)
            {
                date = new DateTime[15];
                var temp =
                    new DataGridViewCheckBoxColumn[15];
                for (int i = 0; i < date.Count(); i++)
                {
                    date[i] = new DateTime(2013, DateTime.Today.Month, (i + 1));
                    temp[i] = new DataGridViewCheckBoxColumn
                    {
                        Name = date[i].Day.ToString(CultureInfo.InvariantCulture),
                        HeaderText = date[i].ToString("dd.MM"),
                        Resizable = DataGridViewTriState.False,
                        Width = 50
                    };
                    //temp[i].ReadOnly = true;
                    dataGridView1.Columns.Add(temp[i]);
                }

            }
            else
            {
                int day = DateTime.DaysInMonth(DateTime.Today.Year, DateTime.Today.Month) - 15;
                date = new DateTime[day];
                int a = 15;
                var temp = new DataGridViewCheckBoxColumn[day];
                for (int i = 0; i < date.Count(); i++)
                {
                    date[i] = new DateTime(2013, DateTime.Today.Month, (a += 1));
                    temp[i] = new DataGridViewCheckBoxColumn
                    {
                        Name = date[i].Day.ToString(),
                        Resizable = DataGridViewTriState.False,
                        HeaderText = date[i].ToString("dd.MM")
                    };
                    //temp[i].ReadOnly = true;
                    dataGridView1.Columns.Add(temp[i]);
                }
            }
            button1.Enabled = true;
            button3.Enabled = false;
        }

        private void comboBox1_SelectedValueChanged(object sender, EventArgs e)
        {
            Type t = typeof (Int32);
            if (comboBox1.SelectedValue.GetType()==t)
            {
                Pol = Convert.ToInt32(comboBox1.SelectedValue);
            }
        }

        private void comboBox2_SelectedValueChanged(object sender, EventArgs e)
        {
            Type t = typeof(Int32);
            if (comboBox2.SelectedValue.GetType()==t)
            {
                YurtId = Convert.ToInt32(comboBox2.SelectedValue);
            }
        }

        protected int YurtId { get; set; }

        private void ImzaCizelgesi_Load(object sender, EventArgs e)
        {
           // button1.Enabled = false;
        }
    }
}

